{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f25f02ce",
   "metadata": {},
   "source": [
    "## 字典的查询\n",
    "\n",
    "> in 方法查询\n",
    "> get 方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1c75810",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu10001\":\"XiaoMing\",\n",
    "    \"stu10002\":\"LiHua\",\n",
    "    \"stu10003\":\"XiaoHong\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5fec6636",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d6e9660c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询key是否在字典中用 in \n",
    "\n",
    "\"stu10001\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6327278c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaoming'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法可以获取字典中key所对应的value\n",
    "\n",
    "info.get(\"stu10001\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a9db897",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "\n",
    "if \"stu10001\" in info:    # 如果stu10001这个key在字典info里面的话\n",
    "    print(info.get(\"stu10001\"))    # 打印所对应的值value\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")     # 如果这个key不在字典info里面，报错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a6c1a63",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "\n",
    "> 1. 字典的循环遍历\n",
    "> 2. fromkeys()方法\n",
    "> 3. setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "868eb662",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "info1 = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "\n",
    "# 创建空列表\n",
    "stu_id_list = []\n",
    "\n",
    "# 循环遍历字典\n",
    "# 循环遍历最大用途就是给所有的值使用同样的 操作/方法\n",
    "for k, v in info1.items():   # items() 方法把字典中每对 key 和 value 组成一个元组，并把这些元组放在列表中返回。\n",
    "    print(k[3:], v)    # k[3:]表示从字典中的键key的第三位开始取\n",
    "    stu_id_list.append(k[3:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20d10aac",
   "metadata": {},
   "source": [
    "**练习：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "66055bfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n",
      "XiaoFeng\n",
      "LiuYu\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Zi', 'Qi', 'Xiao', 'Feng', 'Liu', 'Yu']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_name = []\n",
    "for k,v in info1.items():\n",
    "    print(v)\n",
    "    stu_name.append(v)   # 为空列表增加字典中的值\n",
    "stu_name1 = ''.join(stu_name)   # 将列表转化为字符串\n",
    "stu_name1\n",
    "    \n",
    "import re   # 导入模块\n",
    "re.findall('[A-Z][^A-Z]*', stu_name1)  #利用大写字母分割字符串的方法\n",
    "# chaifen = ([a for a in re.splist(r'([A-Z][a-z]*)',stu_name1)if a])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02b51b67",
   "metadata": {},
   "source": [
    "## fromkeys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3fe731c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "47ae3f65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 123456, 'stu10002': 123456, 'stu10003': 123456}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password = {}\n",
    "stu_password.fromkeys([\"stu10001\",\"stu10002\",\"stu10003\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a8e0e6",
   "metadata": {},
   "source": [
    "## setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "26a393ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "102f3f0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu10004')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f0ddcefd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'aiguo'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu10005','aiguo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ab0bcc58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'xiaoming',\n",
       " 'stu10002': 'lihua',\n",
       " 'stu10003': 'xiaohong',\n",
       " 'stu10004': None,\n",
       " 'stu10005': 'aiguo'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8acdb23c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
